package com.example.exp6_4;

import android.app.Activity;
import android.content.Intent;
import android.icu.util.Output;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.EmptyStackException;
import java.util.StringTokenizer;

public class Second extends Activity {
    private static int nZhengLiyuan=0;
    private static Chain6_4 head;
    private static EditText input1;
    private static EditText delete1;
    private static int inputpoint;
    private static int deletepoint1;
    private static Chain6_4 res1;
    private static Chain6_4 res2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        //接收传过来的字符串并转换成链表
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");

        StringTokenizer stn = new StringTokenizer(message, " ");

        if (!stn.hasMoreElements()) {
            throw new EmptyStackException();
        }

        Chain6_4 chain = new Chain6_4(Integer.parseInt(stn.nextToken()));
        head = chain;
        nZhengLiyuan++;
        while (stn.hasMoreElements()) {
            Chain6_4 newchain = new Chain6_4(Integer.parseInt(stn.nextToken()));
            Connect(head, newchain);
        }

        //输出链表和总数
        Output((TextView) findViewById(R.id.textView1));
        SOUT(R.id.textView2,"Total:" + nZhengLiyuan);

        //由客户输入插入点
/*        ((TextView) findViewById(R.id.textView3)).setText("Please input inputelement and inputpoint:");
        input1 = (EditText) findViewById(R.id.inputpoint);
        StringTokenizer st2 = new StringTokenizer(input1.getText().toString(), " ");
        input(Integer.parseInt(st2.nextToken()), Integer.parseInt(st2.nextToken()));
        if (st2.hasMoreElements()) {
            throw new NullPointerException();
        }*/

          //插入
        SOUT(R.id.textView3,"inputelement=99,inputpoint=3:");
        input(99,3);
        Output((TextView) findViewById(R.id.textView4));

        //由用户输入删除点
/*        ((TextView)findViewById(R.id.textView5)).setText("Please input deletepoint:");
        delete1=(EditText)findViewById(R.id.deletepoint);
        deletepoint1=Integer.parseInt(delete1.getText().toString());
        delete(deletepoint1);
        Output((TextView)findViewById(R.id.textview6));*/

        //删除
        SOUT(R.id.textView5,"deletepoint:4");
        delete(4);
        Output((TextView)findViewById(R.id.textview6));

        //文件读入
        try {
            FileReader();
        } catch (IOException e) {
            e.printStackTrace();
        }
        SOUT(R.id.textView7,"Chain after read:");
        Output((TextView)findViewById(R.id.textView8));

        //删除刚插入的数字1
        delete(nZhengLiyuan-1);
        SOUT(R.id.textView9,"Chain after deleted number1:");
        Output((TextView)findViewById(R.id.textView10));

        //打印元素总数
        SOUT(R.id.textView11,"Total:"+nZhengLiyuan);

        //排序
        Sort();
    }

    public void SOUT(int id,String content){
        ((TextView)findViewById(id)).setText(content);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    public static void Connect(Chain6_4 head, Chain6_4 next){
        Chain6_4 headx=head;

        while (headx.getNext()!=null){
            headx=headx.getNext();
        }
        headx.setNext(next);
        nZhengLiyuan++;
    }

    public void Output(TextView textView){
        Chain6_4 temp=head;
        String output="";
        while (temp!=null){
            output=output+" "+temp.getElement();
            temp=temp.getNext();
        }
        textView.setText(output);
    }

    public static void input(int element, int inpoint){
        Chain6_4 chain2=new Chain6_4(element);
        Chain6_4 temp=head;

        if (inpoint==1){
            chain2.setNext(head);
            Second.head=chain2;
        }
        else if (inpoint>1){
            inpoint--;
            for (int i=1;i< inpoint;i++){
                temp=temp.getNext();
            }
            chain2.setNext(temp.getNext());
            temp.setNext(chain2);
        }
        else {
            throw new NullPointerException();
        }
        nZhengLiyuan++;
    }

    public static void delete(int deletepoint){
        Chain6_4 temp=head;

        if (deletepoint==1){
            Second.head=head.getNext();
        }
        else if (deletepoint>1){
            deletepoint--;
            for (int i=1;i<deletepoint;i++){
                temp=temp.getNext();
            }
            temp.setNext(temp.getNext().getNext());
        }
        else {
            throw new NullPointerException();
        }
        nZhengLiyuan--;
    }

    public void FileReader() throws IOException {
        InputStream inputStream =getAssets().open("test.txt");
        int size=inputStream.available();
        byte [] bytes=new byte[size];
        inputStream.read(bytes);
        inputStream.close();
        String str=new String(bytes);

        StringTokenizer stn=new StringTokenizer(str," ");

        Chain6_4 temp1=new Chain6_4(Integer.parseInt(stn.nextToken()));
        Second.Connect(head,temp1);

        //保留链表
        Chain6_4 head1=head;
        res1=new Chain6_4(head1.getElement());
        while (head1.getNext()!=null){
            res1.setElement(head1.getElement());
            head1=head1.getNext();
            Chain6_4 n=new Chain6_4(head1.getElement());
            res1.setNext(n);
            res1=res1.getNext();
        }

        Chain6_4 temp2=new Chain6_4(Integer.parseInt(stn.nextToken()));
        Second.Connect(head,temp2);
        //保留链表
        res2=new Chain6_4(head1.getElement());
        while (head1.getNext()!=null){
            res2.setElement(head1.getElement());
            head1=head1.getNext();
            Chain6_4 n=new Chain6_4(head1.getElement());
            res2.setNext(n);
            res2=res2.getNext();
        }
    }

    public void Sort(){
        Chain6_4 temp=head;
        Chain6_4 c1,c2;
        int i=0,j=0;
        LinearLayout layout=(LinearLayout)this.findViewById(R.id.Second);

        if (temp==null||temp.getNext()==null){
            TextView textView=new TextView(this);
            Output(textView);
            layout.addView(textView);
        }
        else {
            int x,y;
            for (i=1;i<nZhengLiyuan;i++){
                for (j=i+1;j<=nZhengLiyuan;j++){
                    c1=c2=head;
                    for (x=1;x<i;x++){
                        c1=c1.getNext();
                    }

                    for (y=1;y<j;y++){
                        c2=c2.getNext();
                    }

                    if (c1.getElement()<c2.getElement()){
                        int t;
                        t=c1.getElement();
                        c1.setElement(c2.getElement());
                        c2.setElement(t);

                        TextView textViewi=new TextView(this);
                        textViewi.setText("Context:");
                        layout.addView(textViewi);

                        TextView textViewj=new TextView(this);
                        Output(textViewj);
                        layout.addView(textViewj);

                        TextView textViewk=new TextView(this);
                        textViewk.setText("Total amount:"+nZhengLiyuan);
                        layout.addView(textViewk);
                    }
                }
            }
        }
    }
}
